package com.jackrain.nea.fc.ps.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.util.TypeUtils;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.ps.api.ViewCproDistribQueryCmd;
import com.jackrain.nea.fc.ps.api.ViewCskuQueryCmd;
import com.jackrain.nea.fc.ps.mapper.ViewCproDistribMapper;
import com.jackrain.nea.fc.ps.mapper.ViewCskuQueryMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * “条码档案接口” FC-->货品网站
 *
 * @author WSL
 * @create 2018/1/19
 */
@Slf4j
@Component
@Service(protocol = "dubbo" , validation = "true" , version = "1.0" , group = "ps")
public class ViewCskuQueryCmdImpl extends CommandAdapter implements ViewCskuQueryCmd {
    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

        ValueHolder vh = new ValueHolder();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(map.get("param"), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
        log.info("ViewCskuQueryCmdImpl--param-->{}",param);

        if ((null == param) || (!param.containsKey("IDS")) || param.getJSONArray("IDS").size() <= 0) {
            throw new NDSException("参数格式错误！");
        }

        // Mapper
        ViewCskuQueryMapper viewCskuQueryMapper;
        try {
            viewCskuQueryMapper = ApplicationContextHandle.getBean(ViewCskuQueryMapper.class);
        }catch (Exception e){
            throw new NDSException("服务启动错误！");
        }

        // 根据param查询条件 查询【配销中心商品档案】表
        List<HashMap> list = viewCskuQueryMapper.selectSku(param.getJSONArray("IDS").toArray());
        JSONArray resultJson = new JSONArray();
        for (HashMap hashMap: list) {

            // 配销中心 CP_C_DISTRIB_ID
            String distribId = viewCskuQueryMapper.getDistribEcode(TypeUtils.castToLong(hashMap.get("CP_C_DISTRIB_ID")));
            hashMap.put("CP_C_DISTRIB_ID",distribId);

            // 款号 PS_C_PRO_ID
            String proId = viewCskuQueryMapper.getProEcode(TypeUtils.castToLong(hashMap.get("PS_C_PRO_ID")));
            hashMap.put("PS_C_PRO_ID",proId);

            // 颜色 PS_C_SPEC1OBJ_ID
            String spec1objId = viewCskuQueryMapper.getSpecobjEcode(TypeUtils.castToLong(hashMap.get("PS_C_SPEC1OBJ_ID")));
            hashMap.put("PS_C_SPEC1OBJ_ID",spec1objId);

            // 尺寸编号 PS_C_SPEC2OBJ_ID
            String spec2objId = viewCskuQueryMapper.getSpecobjEcode(TypeUtils.castToLong(hashMap.get("PS_C_SPEC2OBJ_ID")));
            hashMap.put("PS_C_SPEC2OBJ_ID",spec2objId);


            JSONObject jsonObject = JSON.parseObject(JSON.toJSONStringWithDateFormat(new JSONObject(hashMap),"yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue), Feature.OrderedField);
            resultJson.add(jsonObject);

        }
        vh.put("data",resultJson);
        return vh;
    }

}
